package com.lanou.sfu.songforu.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lanou.sfu.songforu.R;
import com.lanou.sfu.songforu.adapter.SearchSingerAdapter;
import com.lanou.sfu.songforu.base.BaseFragment;
import com.lanou.sfu.songforu.constant.Url;
import com.lanou.sfu.songforu.datas.SearchSinger;
import com.lanou.sfu.songforu.singleton.VolleySingleton;
import com.lanou.sfu.songforu.utils.utils.NoScrollListView;

import java.util.List;


/**
 * Created by quyan on 15/10/28.
 * 歌手界面
 */
public class SearchingSingerFragment extends BaseFragment implements AdapterView.OnItemClickListener {
    private TextView singerSongNum;
    private RelativeLayout titleLayout;
    private NoScrollListView singerList;
    private String singerUrl;
    private SearchSingerAdapter adapter;
    private VolleySingleton volleySingleton;
    private List<SearchSinger.DataEntity>data;


    public static SearchingSingerFragment passData(Context mContext, String str) {
        SearchingSingerFragment fragment = new SearchingSingerFragment();
        Bundle bundle = new Bundle();
        bundle.putString("url", str);
        fragment.setArguments(bundle);
        return fragment;

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.search_singer,null);
        singerSongNum = (TextView) view.findViewById(R.id.singer_song_num);
        titleLayout = (RelativeLayout) view.findViewById(R.id.title_layout);
        singerList = (NoScrollListView) view.findViewById(R.id.singer_list);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        volleySingleton = VolleySingleton.getInstance(getActivity());
        singerList.setOnItemClickListener(this);
        Bundle bundle = getArguments();
        String url = bundle.getString("url");
        singerUrl = Url.SEARCH_SINGER_URL + url + Url.SEARCH_SINGER_URL_LAST;
        getInternetData();

    }
    //从网络上拉取数据
    private void getInternetData() {
        StringRequest request = new StringRequest(singerUrl, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                Gson gson = new Gson();
                SearchSinger result = gson.fromJson(s, SearchSinger.class);
                if (result != null && result.getData() != null) {
                    data = result.getData();
                    adapter = new SearchSingerAdapter(getActivity(),data);
                    singerList.setAdapter(adapter);
                    singerSongNum.setText(data.size() + "");
                    titleLayout.setVisibility(View.VISIBLE);
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        });
        volleySingleton.addQueue(request);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        SearchSinger.DataEntity entity = (SearchSinger.DataEntity) parent.getItemAtPosition(position);
        int urlID = entity.get_id();
        SearchContentFragment fragment = new SearchContentFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("id",urlID);
        fragment.setArguments(bundle);
        getFragmentManager().beginTransaction()
                .addToBackStack(null).replace(R.id.fl_main,fragment).commit();
    }
}
